/**
 * 核心技术支持
 * IT渠道网 小程序商城
 * 13873174181 微信同号
 */
import {Nav} from '../../utils/nav.js';
var nav = new Nav();

Page({
    data: {
        pageIndex: 1,
        loadingHidden: false,
        isLoadedAll: false,
        dataArr: [],
        totalPage: 0,
        param: {
            status: null,
            pageNo: 1,
            pageSize: 10
        }
    },
    onLoad: function() {
        this._findMyCoupon();
    },
    /**
     * 获取优惠券数据
     */
    _findMyCoupon: function() {
        var that = this,
            param = this.data.param;
            param.pageNo = this.data.pageIndex;
        nav.findMyCoupon(param, (data)=>{
            data.list.forEach((item) => {
                item.startTime = item.startTime.substring(0,10);
                item.endTime = item.endTime.substring(0,10);
            })
            that.data.dataArr.push.apply(that.data.dataArr, data.list);
            that.setData({
                dataArr: that.data.dataArr,
                totalPage: data.totalPage,
                loadingHidden: true
            })
        })
    },
    /**
     * 状态
     */
    bindStatus: function(e) {
      var that = this,
          param = that.data.param,
          state = nav.getDataSet(e, 'state');
      switch(state) {
          case '-1': param.status = null; that.setData({param: param}); break;
          case '0': param.status = 0; that.setData({param: param}); break;
          case '1': param.status = 1; that.setData({param: param}); break;
          case '2': param.status = 2; that.setData({param: param});
      }
      that.data.pageIndex = 1;
      that.data.dataArr = [];
      that._findMyCoupon();
    },
    /**
     * 监听用户下拉动作 重新刷新用户收藏,回到&page=1
     */
    onPullDownRefresh: function() {
        var that = this;
        that.data.dataArr = [];  // 初始化
        that.data.pageIndex = 1;
        that._findMyCoupon(() => {
            that.setData({
                isLoadedAll: false
            });
            wx.stopPullDownRefresh()
        })
    },
    /**
     * 页面上拉触底事件的处理函数 上拉到底部触发&page=totalPage+1
     */
    onReachBottom: function() {
        if (!this.data.isLoadedAll && this.data.pageIndex < this.data.totalPage) {
            this.data.pageIndex++;
            this._findMyCoupon();
        }
        if (this.data.pageIndex == this.data.totalPage){
            this.setData({
                isLoadedAll: true
            })
        }
    }
})